﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Might.Infrastructure.Services;

namespace Might.Services {
    
    public partial class PopupContentView : UserControl {

        private ModalDialogResult rightBntResult;
        private ModalDialogResult centerBntResult;
        private ModalDialogResult leftBntResult;
        
        private ModalDialogResult result;        
        
        public PopupContentView() {
            InitializeComponent();
        }

        public event EventHandler Click;

        public ModalDialogResult Result {
            get { return result; }
        }

        public void SetButtons( ModalDialogButtons buttons ) {

            ButtonLeft.Click += new RoutedEventHandler( ButtonLeft_Click );
            ButtonCenter.Click += new RoutedEventHandler( ButtonCenter_Click );
            ButtonRight.Click += new RoutedEventHandler( ButtonRight_Click );
            
            if( buttons == ModalDialogButtons.OK ) {
            
                ButtonLeft.Visibility = Visibility.Collapsed;
                ButtonCenter.Visibility = Visibility.Collapsed;
                
                ButtonRight.Content = "OK";
                rightBntResult = ModalDialogResult.OK;
            
            } else if( buttons == ModalDialogButtons.OKCancel || buttons == ModalDialogButtons.RetryCancel
                        || buttons == ModalDialogButtons.YesNo ) {

                ButtonLeft.Visibility = Visibility.Collapsed;
                
                if( buttons == ModalDialogButtons.OKCancel ) {
                    ButtonCenter.Content = "OK";
                    centerBntResult = ModalDialogResult.OK;
                    ButtonRight.Content = "Cancel";
                    rightBntResult = ModalDialogResult.Cancel;
                } else if(  buttons == ModalDialogButtons.RetryCancel ) {
                    ButtonCenter.Content = "Retry";
                    centerBntResult = ModalDialogResult.Retry;
                    ButtonRight.Content = "Cancel";
                    rightBntResult = ModalDialogResult.Cancel;
                } else if(  buttons == ModalDialogButtons.YesNo ) {
                    ButtonCenter.Content = "Yes";
                    centerBntResult = ModalDialogResult.Yes;
                    ButtonRight.Content = "No";
                    rightBntResult = ModalDialogResult.No;
                }
                            
            } else {
            
                if( buttons == ModalDialogButtons.AbortRetryIgnore ) {
                    ButtonLeft.Content = "Abort";
                    leftBntResult = ModalDialogResult.Abort;
                    ButtonCenter.Content = "Retry";
                    centerBntResult = ModalDialogResult.Retry;
                    ButtonRight.Content = "Ignore";
                    rightBntResult = ModalDialogResult.Ignore;
                } else if( buttons == ModalDialogButtons.YesNoCancel ) {
                    ButtonLeft.Content = "Yes";
                    leftBntResult = ModalDialogResult.Yes;                    
                    ButtonCenter.Content = "No";
                    centerBntResult = ModalDialogResult.No;
                    ButtonRight.Content = "Cancel";
                    rightBntResult = ModalDialogResult.Cancel;
                }
                            
            }
            
        }

        public void SetText( string caption , string message ) {
            CaptionTxt.Text = caption;
            MessageTxt.Text = message;
        }

        protected void OnClick() {
            if( Click != null ) {
                Click( this , null );
            }
        }

        void ButtonRight_Click( object sender , RoutedEventArgs e ) {
            result = rightBntResult;
            OnClick();
        }

        void ButtonLeft_Click( object sender , RoutedEventArgs e ) {
            result = leftBntResult;
            OnClick();
        }

        void ButtonCenter_Click( object sender , RoutedEventArgs e ) {
            result = centerBntResult;
            OnClick();
        }
       
    }
}
